package com.presto.directo.mvvm.viewmodel

import android.app.Application
import androidx.lifecycle.*
import com.presto.directo.event.ActionEvent
import kotlinx.coroutines.*

open class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope by MainScope() {

    val actionLiveData = MutableLiveData<ActionEvent>()
    lateinit var owner: LifecycleOwner

    fun setLifecycleOwner(owner: LifecycleOwner) {
        this.owner = owner
    }

    fun showProgress() {
        actionLiveData.value = ActionEvent(ActionEvent.SHOW_PROGRESS)
    }

    fun dismissProgress() {
        actionLiveData.value = ActionEvent(ActionEvent.DISMISS_PROGRESS)
    }

    fun toast(msg: CharSequence?) {
        if (msg.isNullOrBlank()) return
        actionLiveData.value = ActionEvent(ActionEvent.TOAST, msg)
    }

    fun finish() {
        actionLiveData.value = ActionEvent(ActionEvent.FINISH)
    }

    override fun onCleared() {
        super.onCleared()
        cancel()
    }
}